package org.hbhk.hms.mq.tx.mq;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class TypeUtil {
	/**
	 * 获取接口上的泛型T
	 *
	 * @param o     接口
	 * @param index 泛型索引
	 */
	public static Class<?> getInterfaceT(Object o, int index) {
		Type[] types = o.getClass().getGenericInterfaces();
		ParameterizedType parameterizedType = (ParameterizedType) types[index];
		Type type = parameterizedType.getActualTypeArguments()[index];
		return checkType(type, index);

	}

	/**
	 * 获取类上的泛型T
	 *
	 * @param o     接口
	 * @param index 泛型索引
	 */
	public static Class<?> getClassT(Object o, int index) {
		Type type = o.getClass().getGenericSuperclass();
		if (type instanceof ParameterizedType) {
			ParameterizedType parameterizedType = (ParameterizedType) type;
			Type actType = parameterizedType.getActualTypeArguments()[index];
			return checkType(actType, index);
		} else {
			String className = type == null ? "null" : type.getClass().getName();
			throw new IllegalArgumentException(
					"Expected a Class, ParameterizedType" + ", but <" + type + "> is of type " + className);
		}
	}

	private static Class<?> checkType(Type type, int index) {
		if (type instanceof Class<?>) {
			return (Class<?>) type;
		} else if (type instanceof ParameterizedType) {
			ParameterizedType pt = (ParameterizedType) type;
			Type t = pt.getActualTypeArguments()[index];
			return checkType(t, index);
		} else {
			String className = type == null ? "null" : type.getClass().getName();
			throw new IllegalArgumentException(
					"Expected a Class, ParameterizedType" + ", but <" + type + "> is of type " + className);
		}
	}

}
